<html>
<head><title>CS143 Project 1B</title></head>
<body>
<h1>CS 143 Film Database</h1>
CS 143 Project 1B Solution<br />
Author: Ervin Sukardi  (203 808 277)<br />
Version 1.0<br />
<br />

<b>Insert your SQL Query:</b><br />
<form method="GET">
<textarea name="query" cols="60" rows="8">
</textarea>
<br />
<input type="submit" value="Submit" />
</form>

<h3>Result:</h3>

<?php

// Open MySQL connection and access the database
$db_connection = mysql_connect("localhost", "cs143", "");
mysql_select_db("CS143", $db_connection);

// Perform query and check
$query = $_GET["query"];
$result = mysql_query($query, $db_connection);
echo 'Performing the query:<br />'.$query.'<br /><br />';

// Basic query integrity check
if(!$result)
{
	echo '<i>I\'m sorry, we were unable to serve that query.<br />';
}

// If query succeeds, parse result into a table
else
{
	echo '<i>Successfully performed query!</i><br />';

	// Start table and create header
	echo '<table border=1 cellspacing=1 cellpadding=2>';	
	$rowsize = 0;
	echo '<tr align=center>';
	while($rowsize < mysql_num_fields($result))
	{
		$meta = mysql_fetch_field($result, $rowsize);
		echo '<td><b>'.$meta->name.'</b></td>';
		$rowsize++;
	}
	echo '</tr>';
	
	// Insert results
	while($row = mysql_fetch_row($result))
	{
		echo '<tr align=center>';
		for($k = 0; $k < $rowsize; $k++)
		{
			echo '<td>'.$row[$k].'</td>';
		}
		echo '</tr>'; 
	}
	echo '</table>';
	
	// Free results
	mysql_free_result($result);
}

// Close connection
mysql_close($db_connection);
?>
</body>
</html>